package com.magic.smartcity.view.more.adapters

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.magic.smartcity.R
import com.magic.smartcity.beans.ParkBean
import com.magic.smartcity.databinding.ItemParkHomeListBinding
import com.magic.smartcity.utils.Constant

/**
 * @description:
 * @create: Create by Magic on 2022/2/23.
 */
class ParkHomeListAdapter : RecyclerView.Adapter<ParkHomeListAdapter.ItemHolder>() {

    private val dataList: MutableList<ParkBean> = mutableListOf()

    fun setData(data: List<ParkBean>) {
        dataList.clear()
        dataList.addAll(data)
        notifyDataSetChanged()
    }

    class ItemHolder(binding: ItemParkHomeListBinding) : RecyclerView.ViewHolder(binding.root) {
        val binding = binding
    }

    private val TAG: String = "ParkHomeListAdapter"
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val inflate = DataBindingUtil.inflate<ItemParkHomeListBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_park_home_list,
            parent,
            false
        )
        return ItemHolder(inflate)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.binding.parkDetail = dataList[position]
        Glide.with(holder.binding.imageView7).load(Constant.Service_IP + dataList[position].imgUrl)
            .error(R.color.gray).into(holder.binding.imageView7)
        holder.binding.root.setOnClickListener{
            val bundle = Bundle()
            bundle.putInt("parkId",dataList[position].id)
            Navigation.findNavController(it).navigate(R.id.action_parkHomeFragment_to_parkDetailFragment,bundle)
        }
    }

    override fun getItemCount()=dataList.size
}